package com.wushijia.algorithm;

import com.wushijia.test.Solution;
import java.util.LinkedList;
import java.util.Queue;
import javax.sound.midi.Soundbank;

/**
 * 二叉树左右视图
 *
 * @author Administrator
 * @date 2021-06-28 14:06
 */
public class BinaryTreeView {

  public static void main(String[] args) {

    ListNode root = new ListNode(1);
    ListNode r2 = new ListNode(2);
    ListNode r3 = new ListNode(3);
    ListNode r4 = new ListNode(4);
    ListNode r5 = new ListNode(5);
    ListNode r6 = new ListNode(6);
    ListNode r7 = new ListNode(7);
    ListNode r8 = new ListNode(8);
    ListNode r9 = new ListNode(9);
    ListNode r10 = new ListNode(10);
    ListNode r11 = new ListNode(11);
    ListNode r12 = new ListNode(12);
    ListNode r13 = new ListNode(13);
    ListNode r14 = new ListNode(14);
    ListNode r15 = new ListNode(15);
    root.left = r2;
    root.right = r3;
    r2.left = r4;
    r2.right = r5;
    r3.left = r6;
    r3.right = r7;
    r4.left = r8;
    r4.right = r9;
    r9.left = r10;
    r6.left = r11;
    r7.right = r12;
    r12.right = r13;
    r13.right = r14;
    r14.right = r15;
    new BinaryTreeView().leftView(root);
  }

  public void leftView(ListNode root) {
    Queue<ListNode> curr = new LinkedList<>();
    Queue<ListNode> next = new LinkedList<>();
    curr.add(root);
    while (!curr.isEmpty() || !next.isEmpty()) {

      Queue<ListNode> currTemp;
      Queue<ListNode> nextTemp;
      if (!curr.isEmpty()) {
        currTemp = curr;
        nextTemp = next;
      } else {
        currTemp = next;
        nextTemp = curr;
      }
      System.out.println(currTemp.peek().val);

      while (!currTemp.isEmpty()) {
        ListNode temp = currTemp.poll();
        if (temp.left != null) {
          nextTemp.add(temp.left);
        }
        if (temp.right != null) {
          nextTemp.add(temp.right);
        }
      }
    }


  }


}

class ListNode {

  int val;
  ListNode left;
  ListNode right;

  public ListNode(int val) {
    this.val = val;
  }
}
